package com.jiantong.admin.job;

/**
 * Created by LiuDongFang on 2017/9/18.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.jiantong.common.util.MD5Util;
import com.jiantong.user.model.*;
import com.jiantong.user.dto.InsertUserGov;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;


public class Insert {

    //记录类的输出信息­
    static Log log = LogFactory.getLog(Insert.class);


    /**
     * 遍历表格数据
     * @param file
     * @return
     */
    public static List<InsertUserGov> insertUser(MultipartFile file ) throws IOException {

        List<InsertUserGov> insertUserGovList=new ArrayList<>();

        try {
            // 创建对Excel工作簿文件的引用­
            HSSFWorkbook wookbook = new HSSFWorkbook(file.getInputStream());

            // 在Excel文档中，第一张工作表的缺省索引是0
            // 其语句为：HSSFSheet sheet = workbook.getSheetAt(0);
            HSSFSheet sheet = wookbook.getSheet("Sheet1");
            if (sheet==null){
                return null;
            }
            //获取到Excel文件中的所有行数­
            int rows = sheet.getPhysicalNumberOfRows();
            //遍历行­
            for (int i = 1; i < rows; i++) {
                // 读取左上端单元格
                HSSFRow row = sheet.getRow(i);
                // 行不为空­
                if (row != null&&row.getCell(0)!=null) {
                    //获取到Excel文件中的所有的列­
                    int cells = row.getPhysicalNumberOfCells();
                    String value = "";
                    //遍历列­

                    for (int j = 0; j < cells; j++) {
                        //获取到列的值­

                        HSSFCell cell = row.getCell(j);;
                        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        if (cell != null) {
                            switch (cell.getCellType()) {

                                case HSSFCell.CELL_TYPE_FORMULA:
                                    break;
                                case HSSFCell.CELL_TYPE_NUMERIC:
                                    value += String.valueOf(cell.getNumericCellValue()) + ",";
                                    break;
                                case HSSFCell.CELL_TYPE_STRING:
                                    value += cell.getStringCellValue() + ",";
                                    break;
                                default:
                                    value += "0";
                                    break;
                            }
                        }
                    }
                    // 将数据插入到mysql数据库中­
                    String[] val = value.split(",");
                    //用户登录表
                    User user = new User();
                    user.setName(val[0]);
                    user.setEmail(val[1]);
                    user.setMobile(val[2]);
                    user.setPassword(MD5Util.encrypt(val[3]));
                    user.setAreaLevel(Integer.parseInt(val[4]));
                    user.setStatus(1);
                    user.setType(2);
                    //用户详情表
                    UserProfile userProfile = new UserProfile();
                    userProfile.setGender(Integer.parseInt(val[5]));
                    userProfile.setRealName(val[0]);
                    userProfile.setProvinceId(Integer.parseInt(val[6]));
                    userProfile.setProvince(val[7]);
                    userProfile.setCityId(Integer.parseInt(val[8]));
                    userProfile.setCity(val[9]);
                    userProfile.setRegionId(Integer.parseInt(val[10]));
                    userProfile.setRegion(val[11]);
                    userProfile.setStreetId(Integer.parseInt(val[12]));
                    userProfile.setStreet(val[13]);
                    userProfile.setVillageId(Integer.parseInt(val[14]));
                    userProfile.setVillage(val[15]);
                    //机构用户
                    OrganizationUser organizationUser = new OrganizationUser();
                    organizationUser.setOrganizationName(val[16]);
                    organizationUser.setOrganizationId(Long.parseLong(val[17]));
                    organizationUser.setOrganizationType(Integer.parseInt(val[18]));
                    organizationUser.setRoleName(val[19]);
                    //角色表
                    AppUserRole appUserRole = new AppUserRole();
                    appUserRole.setAppRole(Integer.parseInt(val[20]));
                    appUserRole.setStatus(1);
                    //部门用户表
                    DepartmentUser departmentUser = new DepartmentUser();
                    departmentUser.setUserName(val[0]);
                    departmentUser.setDepartmentId(Long.parseLong(val[21]));
                    departmentUser.setDepartmentName(val[22]);
                    departmentUser.setDepartmentType(Integer.parseInt(val[23]));
                    departmentUser.setStatus(1);
                    departmentUser.setEmpType(1);
                    //组装数据
                    InsertUserGov insertUserGov=new InsertUserGov();
                    insertUserGov.setUser(user);
                    insertUserGov.setUserProfile(userProfile);
                    insertUserGov.setAppUserRole(appUserRole);
                    insertUserGov.setDepartmentUser(departmentUser);
                    insertUserGov.setOrganizationUser(organizationUser);
                    insertUserGovList.add(insertUserGov);

                }
            }
            return insertUserGovList;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
